
package com.cbgolf.oa.widget;

import android.content.Context;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
public class MyMarkerView extends MarkerView {

    private TextView tvContent;

    public MyMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);

        tvContent = (TextView) findViewById(R.id.tvContent);
    }

    // callbacks everytime the MarkerView is redrawn, can be used to update the
    // content (user-interface)
    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

//            tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
            tvContent.setText(formatNumber(ce.getHigh()));
        } else {
            tvContent.setText(formatNumber(e.getY()));
//            tvContent.setText("" + Utils.formatNumber(e.getY(), 0, true));
        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }

    public String formatNumber(float num) {
        String result = "0";
        if (num <= 0) {
            return result;
        }
        BigDecimal b = new BigDecimal(num);
        float f = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        DecimalFormat df = new DecimalFormat("#.##");
        result = df.format(f);
        return result;
    }
}
